import { defineStore } from 'pinia'
import { getProductList,getTotal,postUpdateFlag,getCategory,search } from '../API/product-API'

export const useStore = defineStore('useStore',{
    state:() =>{
        return {
            proList:[],
            page:1,
            total:0,
            isCollapse:false,
            categoryList:['全部'],
            searchList:[]
        }
    },
    getters:{
        
    },
    actions:{
        // 获取产品列表
        async handelProList() {
            const res = await getProductList(this.page,10);
            console.log(res.data);
            if(res.code === '200') {
                this.proList = res.data
            }
        },
        // 分页
        handelPage(page) {
            this.page = page
            // console.log("store",this.page);
            this.handelProList()
        },
        // 产品总量
        async handelTotal() {
            const res = await getTotal()
            // console.log(res);
            this.total = res.data
        },
        //秒杀,推荐
        async handelFlag(proid,type,flag) {
            const res = await postUpdateFlag(proid,type,flag)
            if(res.code === '200') {
                this.handelProList()
            }
        },
        // 分类
        async handelCategory() {
            const res = await getCategory()
            // console.log(res.data);
            this.categoryList.push(...res.data)
        },
        // 筛选
        async handelSearch(text=true,val=true) {
            if(text === '全部') {
                text = ''
            }
            const res = await search(text,val)
            console.log('筛选:',res);
            this.searchList = res.data
        }
    }
})